<?php


namespace app\common\model\base;

use app\common\model\BaseModel;

class DictItemModel extends BaseModel
{

    protected $table = 'dict_item';

    /**
     * 获取字典值
     * @param $classCode
     * @param $dictCode
     * @return mixed
     */
    public function getDictValue($classCode,$dictCode){
        return $this->where('class_code',$classCode)->where('dict_code',$dictCode)->value('dict_value');
    }

    /**
     * 按分类获取数据
     * @param $classCode
     * @return \think\Collection
     * @throws \Exception
     */
    public function getList($classCode){
        return $this->where('class_code',$classCode)->order('show_order asc')->select();
    }
    /**
     * 添加字典
     * @param $data
     * @return DictItemModel
     * @throws \Exception
     */
    public function add($data){
        $info = $this->where('class_code',$data['class_code'])->where('dict_code',$data['dict_code'])->find();
        if($info) exception('该编码已存在');
        return self::create($data);
    }

    /**
     * 修改字典
     * @param $data
     * @return DictItemModel
     * @throws \Exception
     */
    public function edit($data){
        $info = $this->where('id',$data['id'])->find();
        if($info['dict_code']!=$data['dict_code']){
            $info = $this->where('class_code',$info['class_code'])->where('dict_code',$data['dict_code'])->find();
            if($info) exception('该编码已存在');
        }
        return self::update($data);
    }

    /**
     * 删除字典
     * @param $id
     * @throws \Exception
     */
    public function del($id){
        $this->where('id','in',$id)->delete();
    }
}